package boerse.main.StockPriceProvider;

import boerse.main.Asset.Share;
import boerse.main.Exceptions.NegativeValueException;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Hashtable;
import java.util.List;
import java.util.Scanner;

/**
 * Take historical values from a cvs data and update per tick to them
 * @author Domi
 *
 */
public class HistoricalStockPriceProvider extends StockPriceProvider{
    private Hashtable<String, List<Long>> historicalValues;
    private int historicalCounter;

    

    public HistoricalStockPriceProvider() {
        super();
        startUpdate();
        historicalCounter = 0;
    }



    @Override
    protected void updateShareRate(Share share) {
        //To change body of implemented methods use File | Settings | File Templates.

        List<Long> v = historicalValues.get(share.getName());
        Object[] s =   v.toArray();
        if(historicalCounter >= v.size()-1)
            historicalCounter = 0;
        
        
        duUpdateRate(share,(long)s[historicalCounter]);

    }

    public void historicalSetup() throws IOException {
        historicalCounter = 0;
        historicalValues = new Hashtable<String, List<Long>>();

        Object [] shares = allShare.keySet().toArray();
        for (int i = 0; i < shares.length; i++) {
            historicalValues.put((String)shares[i],createList(allShare.get(shares[i])));
        }
    }

    private List<Long> createList(Share share) throws IOException {
        FileReader fr = new FileReader(allShare.get(share.getName()).getData());
        BufferedReader br = new BufferedReader(fr);

        List<Long> values = new ArrayList<Long>();


        String input = br.readLine();
        input = br.readLine();
       
        while (input != null) {
        //    s.replaceAll("[\\D]", "");
      //      String[] input = s.split(",");
        	try{
        	
	        	String[] inputValues = input.split(",");
	        	
//	        	for (int j = 1; j < inputValues.length-2; j++) {
//	        	       	
//		        	String s = inputValues[j];
//		            float d = Float.parseFloat(s)*100;
//		        	
//		        	values.add((long) d);
//	        	}
	        	String s = inputValues[4];
	        	float d = Float.parseFloat(s)*100;
	        	values.add((long)d);
	        	
	        	input = br.readLine();
        	}catch (NumberFormatException e) {
        		e.printStackTrace();
        	}
        }

        br.close();

        return values;

    }

    private void duUpdateRate(Share share, long price) {
        try {
            share.setPrice(price);
        } catch (NegativeValueException e) {
            return;
        }
    }

    public void updateShareRates() {
        for (Share s : allShare.values()) {
            updateShareRate(s);
        }
        countHistorical();
    }

    private void countHistorical() {
        historicalCounter++;
    }
}
